package com.zxy.zxyrpc.proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * mock服务代理
 *
 * @author zxy
 * @since 2025/5/11
 **/
@Slf4j
public class MockServiceProxy implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 根据方法返回值类型返回默认数据
        Class<?> returnType = method.getReturnType();
        log.info("mock invoke {}", method.getName());
        return getDefaultObject(returnType);
    }

    /**
     * 生成指定类型的默认值
     * 实现（1.if判断/switch
     *      2.枚举
     *      3.静态map
     */
    private Object getDefaultObject(Class<?> type) {
        // 如果返回值是基本数据类型
        if (type.isPrimitive()) {
            if (type == boolean.class)
                return false;
            else if (type == short.class)
                return (short) 0;
            else if (type == int.class)
                return 0;
            else if (type == long.class)
                return 0L;
            else if (type == double.class)
                return 0d;
            else if (type == float.class)
                return 0f;
            else if (type == char.class)
                return "默认值";
        }
        // 对象返回null
        return null;
    }
}
